#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "DialogView.h"

#include "Class.h"
#include "OrdColl.h"

//---- DialogView --------------------------------------------------------------

NewAbstractMetaImpl(DialogView,View, (TP(dialogRoot)));

DialogView::DialogView(EvtHandler *eh) : View(eh, Point(3000))
{
	dialogRoot= 0;
}

DialogView::~DialogView()
{
	SafeDelete(dialogRoot);
}

void DialogView::ExtentChanged(VObject*)
{
	Point oldextent(GetExtent());
	CalcLayout(TRUE);
	if (oldextent != GetExtent())
		View::ExtentChanged(this);
}

VObject *DialogView::GetDialogRoot()
{
	if (dialogRoot == 0)
		SetDialog(DoCreateDialog(), FALSE);
	return dialogRoot;
}

void DialogView::Open(bool mode)
{
	View::Open(mode);
	GetDialogRoot();
	if (dialogRoot)
		dialogRoot->Open(IsOpen());
}

VObject *DialogView::DoCreateDialog()
{
	return 0;
}

Metric DialogView::GetMinSize()
{
	Metric m;

	GetDialogRoot();
	if (dialogRoot) {
		m= dialogRoot->GetMinSize();
		SetFlag(eVObjHFixed, dialogRoot->TestFlag(eVObjHFixed));
		SetFlag(eVObjVFixed, dialogRoot->TestFlag(eVObjVFixed));
	}
	return m;
}

VObject *DialogView::SetDialog(VObject *dia, bool redraw)
{
	VObject *olddialog= 0;
	if (dia) {
		olddialog= dialogRoot;
		dialogRoot= dia;
		dialogRoot->SetContainer(this);
		dialogRoot->Enable();
		CalcLayout(redraw);
	}
	return olddialog;
}

void DialogView::CalcLayout(bool redraw)
{
	if (dialogRoot) {
		SetExtent(GetMinSize().Extent());
		//SetOrigin(GetOrigin());
		View::SetOrigin(GetOrigin());
		if (dialogRoot)
			dialogRoot->SetOrigin(GetOrigin());
		if (redraw)
			ForceRedraw();
	}
}

void DialogView::SetOrigin(Point at)
{
	View::SetOrigin(at);
	/*
	if (dialogRoot)
		dialogRoot->SetOrigin(at);
	*/
}

void DialogView::SetExtent(Point e)
{
	View::SetExtent(e);
	if (dialogRoot)
		dialogRoot->SetExtent(e);
}

VObject *DialogView::Detect(BoolFun find, void *arg)
{
	VObject *v;
	if (dialogRoot && (v= dialogRoot->Detect(find, arg)))
		return v;
	return VObject::Detect(find, arg);
}

Command *DialogView::DispatchEvents(Point lp, Token &t, Clipper *vf)
{
	if (dialogRoot)
		return dialogRoot->Input(lp, t, vf);
	return View::DispatchEvents(lp, t, vf);
}

void DialogView::CollectParts(Collection *col)
{
	View::CollectParts(col);
	col->Add(dialogRoot);
}

void DialogView::EnableItem(int id, bool b)
{
	VObject *vop;
	if (vop= FindItem(id))
		vop->Enable(b, TRUE);
}

VObject *DialogView::FindItem(int id)
{
	if (dialogRoot)
		return dialogRoot->FindItem(id);
	return 0;
}

void DialogView::Draw(Rectangle r)
{
	if (dialogRoot)
		dialogRoot->DrawAll(r, FALSE);
}

